namespace Org.Loon.Framework.Xna.Game.Action.Sprite
{

    using System;
    using System.Collections.Generic;
    using Org.Loon.Framework.Xna.Game.Core;
    using Org.Loon.Framework.Xna.Game.Core.Graphics;
    using Org.Loon.Framework.Xna.Game.Core.Input;
    using Org.Loon.Framework.Xna.Game.Core.Graphics.OpenGL;
    using Org.Loon.Framework.Xna.Game.Core.Geom;
    using Microsoft.Xna.Framework;

    public class Strap : LObject, ISprite
    {

        private float[] xs;

        private float[] ys;

        private int dx, dy;

        private int px, py;

        private bool visible, isClose;

        private bool initialized;

        private bool streached;

        private int size;

        private int count;

        private LColor color;

        public Strap():this(LColor.red)
        {
            
        }

        public Strap(LColor c):this(4, c)
        {
            
        }

        public Strap(int s, LColor c)
        {
            this.xs = new float[4];
            this.ys = new float[4];
            this.count = 3;
            this.alpha = 1f;
            this.size = s;
            this.color = c;
            this.visible = true;
        }

        public override void Update(long elapsedTime)
        {
            if (!this.initialized || isClose)
            {
                Initialize();
            }
            if (this.streached)
            {
                Update();
                this.dx = (this.px - X());
                this.dy = (this.py - Y());
                this.xs[this.count] = (this.size + this.dx);
                this.ys[this.count] = (this.size + this.dy);
            }
        }

        public void OnDrag(LTouch e)
        {
            OnDrag(e.GetX(), e.GetY());
        }

        public void OnDrag(float x, float y)
        {
            this.px = (int)x;
            this.py = (int)y;
            this.streached = true;
            if (GetX() - this.px < 0)
            {
                if (GetY() - this.py < 0)
                {
                    this.count = 2;
                }
                else
                {
                    this.count = 1;
                }
            }
            else if (GetY() - this.py < 0)
            {
                this.count = 3;
            }
            else
            {
                this.count = 0;
            }
        }

        public void Update()
        {
            this.xs[0] = -this.size;
            this.xs[1] = +this.size;
            this.xs[2] = +this.size;
            this.xs[3] = -this.size;
            this.ys[0] = -this.size;
            this.ys[1] = -this.size;
            this.ys[2] = +this.size;
            this.ys[3] = +this.size;
        }

        public void Reset()
        {
            this.initialized = false;
        }

        public void Initialize()
        {
            this.xs[0] = -this.size;
            this.xs[1] = +this.size;
            this.xs[2] = +this.size;
            this.xs[3] = -this.size;
            this.ys[0] = -this.size;
            this.ys[1] = -this.size;
            this.ys[2] = +this.size;
            this.ys[3] = +this.size;
            this.initialized = true;
            this.streached = false;
        }

        public virtual void CreateUI(GLEx g)
        {
            if (!visible || isClose)
            {
                return;
            }
            if (alpha > 0 && alpha < 1)
            {
                g.SetAlpha(alpha);
            }
            g.Translate(X(), Y());
            g.FillPolygon(this.xs, this.ys, 4, color.Color);
            g.DrawPolygon(this.xs, this.ys, 4, Color.Black);
            g.Translate(-X(), -Y());
            if (alpha > 0 && alpha < 1)
            {
                g.SetAlpha(1);
            }
        }

        public override int GetWidth()
        {
            return size;
        }

        public override int GetHeight()
        {
            return size;
        }

        public virtual LTexture GetBitmap()
        {
            return null;
        }

        public virtual RectBox GetCollisionBox()
        {
            return GetRect(X(), Y(), size, size);
        }

        public virtual bool IsVisible()
        {
            return visible;
        }

        public virtual void SetVisible(bool visible_0)
        {
            this.visible = visible_0;
        }

        public bool IsClose()
        {
            return isClose;
        }

        public virtual void Dispose()
        {
            this.isClose = true;
            if (xs != null)
            {
                xs = null;
            }
            if (ys != null)
            {
                ys = null;
            }
        }
	
    }
}
